const API = require('../../pages/api/index')
const {
  typeJson
} = require('../../utils/typejson')
let prolist = wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist'))

const {
  get,
  post,
  put
} = require('../../request/index')


Page({

  /**
   * 页面的初始数据
   */
  data: {
    pulldown: "",
    pullup: 2,
    addButton: false,
    active: 0,
    prolist: wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist')),

    alarm1: [],
    alarm2: [],
    alarm3: [],
    alarm4: [],
    flowDetails: [],
    flowDetailsItem: {},
    showCheck:false, // 显示审核人员信息
    show: false,
    show1: false,
    flowNo: null,
    id: null,
    sign: null,
    content: "",
    jsonType: [{
      label: "通过",
      value: 3
    }, {
      label: "不通过",
      value: 4
    }],
    jsonTypea: [{
      label: "通过",
      value: 1
    }, {
      label: "不通过",
      value: 0
    }],
    show2: false,
    activeId: [],
    nameCar: [],
    activeIdPopup: [],
    max: 100,
    deptTreeList: [],
    wuanlist: [], // 人员数据
    wuanlistCache: [], // 模糊搜索数据
    peopleValue: '', // 模糊搜索数据关键词
    showPeople: false,
    responUserId: null,
    people: "",
    rectPhoto: "",
    measure: "",
    flag: 1,
    category: null,
    titleName: "",
    approStatus: null,
    pageNum: 1,
    pageSize: 99999999999,
    desci: null,
    inspectedUsers: null,
    position: null,
    level: null,
    photo: null,
    po: "https://img.yzcdn.cn/vant/cat.jpeg"


  },
  selent() {
    this.setData({
      show2: true
    })
    this.postbalstWork();
  },
  rect(e) {
    let {
      flowNo,
    } = e.target.dataset.flowdetails

    this.setData({
      show3: true,
      flowNo
    })
  },
  add() {
    wx.navigateTo({
      url: `/pages/dynamic/dynamic`,
    })
  },
  onChangeInput(e) {
    this.setData({
      [e.target.dataset.lable]: e.detail
    })
  },
  // 去审核
  examine(e) {
    let {
      flowNo,
      desci,
      inspectedUsers,
      position,
      level,
      photo,
      id
    } = e.target.dataset.flowdetails
    this.setData({
      flowNo,
      desci,
      inspectedUsers,
      position,
      level,
      photo,
      id,
      show1: true
    })
  },
  canvasChang(e) {
    this.setData({
      sign: e.detail
    })
  },
  changeTextarea(e) {
    this.setData({
      content: e.detail
    })
  },
  changeStatus(e) {

    this.setData({
      status: e.detail
    })
  },
  changeFlag(e) {
    this.setData({
      flag: e.detail
    })
  },
  // 确定修改审批
  smt() {
    let {
      flowNo,
      id,
      sign,
      status,
      content,
      prolist,
      responUserId,
      nameCar
    } = this.data
    post('/app/dangerhidden/update', {
      flowNo,
      id,
      sign,
      status,
      content,
      auditUserId: prolist.userId,
      responUserId,
      deptName: nameCar.toString()
    }).then(res => {
      if (res.code == 200) {
        this.setData({
          show1: false,
          flowNo: ""
        })
        this.change3()
      }
    })
  },
  //去整改
  smta() {
    let {
      flowNo,
      rectPhoto,
      measure,
      flag
    } = this.data
    put('/app/dangerhidden', {
      flowNo,
      rectPhoto,
      measure,
      flag
    }).then(res => {
      if (res.code == 200) {
        this.setData({
          show3: false,
          flowNo: ""
        })
        this.change2()
      }
    })
  },
  onChange(event) {
    console.log(event);
    switch (event.detail.index) {
      case 0:
        this.change1()
        break;
      case 1:
        this.change3()
        break;
      case 2:
        this.change2()
        break;
      case 3:
        this.change4()
        break;

      default:
        break;
    }
  },
  onChangegroup(event) {
    this.setData({
      responUserId: event.detail,
    });
  },

  //隐患发现
  async change1() {
    let {
      pageNum,
      pageSize,
      prolist
    } = this.data
    const res = await post('/app/dangerhidden/getList', {
      userId: prolist.userId,
      type: 5,
      pageNum,
      pageSize,
      category: this.data.category
    })
    console.log(res);
    if (res.code == 200) {
      this.setData({
        alarm1: res.rows
      })
    }

  },

  // 已审核
  async change2() {
    let {
      pageNum,
      pageSize,
      prolist,
      category
    } = this.data
    const res = await post('/app/dangerhidden/getList', {
      userId: prolist.userId,
      type: 5,
      approStatus: 2,
      pageNum,
      pageSize,
      category
    })
    if (res.code == 200) {
      this.setData({
        alarm2: res.rows
      })
    }
  },

  //待审核
  async change3() {
    console.log(111);
    let {
      pageNum,
      pageSize,
      category,
      prolist
    } = this.data
    const res = await get(`/app/dangerhidden/listByMyslef/${prolist.userId}/${category}?pageNum=${pageNum}&pageSize=${pageSize}`)
    if (res.code == 200) {
      this.setData({
        alarm3: res.rows
      })
    }
  },
  //已整改
  async change4() {
    let {
      prolist,
      pageNum,
      pageSize,
      category
    } = this.data
    const res = await post('/app/dangerhidden/listRectification', {
      userId: prolist.userId,
      type: 5,
      pageNum,
      pageSize,
      approStatus: 2,
      category
    })
    if (res.code == 200) {
      this.setData({
        alarm4: res.rows
      })
    }
  },
  // 查看
  look(e) {
    let item = e.target.dataset.flowdetails
    console.log(item.flowDetailsItem);
    item.limitedTime = item.limitedTime ? item.limitedTime.slice(0, 10) : ''
    this.setData({
      show: true,
      flowDetailsItem: item,
      approStatus: item.approStatus,
      flowDetails: item.flowDetails
    })


  },
  // 显示审核信息
  showDetail() {
    this.setData({
      showCheck: !this.data.showCheck
    })
  },
  onClickNav({
    detail = {}
  }) {
    this.setData({
      mainActiveIndex: detail.index || 0,
    });
  },

  onClickItem({
    detail = {}
  }) {
    console.log(detail);
    const {
      activeId,
      nameCar
    } = this.data;

    const index = activeId.indexOf(detail.id);
    if (index > -1) {
      activeId.splice(index, 1);
      nameCar.splice(index, 1)
    } else {
      activeId.push(detail.id);
      nameCar.push(detail.text)
    }
    this.setData({
      activeId,
      nameCar,
      people: "",
      deptName: detail.text
    });



  },
  toggle(event) {
    console.log(event);
    const {
      name,
      text
    } = event.currentTarget.dataset;
    this.setData({
      responUserId: name,
      people: text,
      showPeople: false
    });
  },
  onChangeInput(e) {
    this.setData({
      [e.target.dataset.lable]: e.detail
    })
  },
  async postbalstWork() {
    let res = await get(`/system/user/list?pageNum=1&pageSize=999`)
    this.setData({
      wuanlist: res.rows
    })
  },
  peopleBtn() {
    this.setData({
      showPeople: true
    })
  },
  onClosePeople() {
    this.setData({
      showPeople: false
    })
  },
  onChangePeople(name) {
    let newListData = []; // 用于存放搜索出来数据的新数组
    if (name.detail) {
      this.data.wuanlist.forEach((item) => {
        if (item.nickName.indexOf(name.detail) !== -1) {
          console.log(item);
          newListData.push(item);
        }
      });
    }
    this.setData({
      wuanlistCache: newListData
    })
  },
  onClose() {
    this.setData({
      show: false,
      show1: false,
      show2: false,
      show3: false
    })
  },
  onClose2() {
    this.setData({
      show2: false
    })
  },

  onClose3() {
    this.setData({
      show3: false
    })
  },

  changeUpint(e) {
    this.setData({
      rectPhoto: e.detail
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(option) {
    let obj = wx.getStorageSync('obj') == '' ? wx.getStorageSync('obj') : JSON.parse(wx.getStorageSync('obj'))
    console.log(obj);
    this.setData({
      category: Number(obj.category),
      titleName: obj.title
    })
    this.getDeptTree();
    if (option.active) {
      this.change3()
      this.setData({
        active: 1
      })
    } else {
      this.change1()
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  async getDeptTree() {
    let res = await get('/system/user/deptTree');

    if (res.code == 200) {
      let newJsonParam = JSON.parse(JSON.stringify(res.data).replace(/label/g, "text"));
      this.setData({
        deptTreeList: newJsonParam
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    this.setData({
      showCom: false
    });
    this.setData({
      showCom: true
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    return
    console.log('触底了----');
    // this.setData({
    //   pullup: this.data.pullup + 1
    // })
  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})